package com.zfast.config.controller;

import cn.hutool.core.date.DateUtil;
import com.zfast.config.constant.SessionKeyConst;
import com.zfast.config.entity.User;
import com.zfast.config.service.AdminService;
import com.zfast.config.vo.CommonResult;
import com.zfast.config.vo.Data;
import com.zfast.config.vo.PageBean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname AdminController
 * @Description 用户管理
 * @Date 2019/7/17 17:00
 * @Created by WDD
 */
@Controller
@RequestMapping("/manager")
public class AdminController {

    @Resource
    private CommonResult commonResult;

    @Resource
    private AdminService adminService;
    @Resource
    private BCryptPasswordEncoder passwordEncoder;

    /**
     * 跳转管理员页面
     *
     * @return
     */
    @GetMapping("/admin")
    public String admin() {
        return "manager/admin/adminList";
    }

    /**
     * 异步加载管理员列表
     *
     * @param pageno
     * @param pagesize
     * @param username
     * @param phone
     * @param email
     * @return
     */
    @RequestMapping("/adminList")
    @ResponseBody
    public Object adminList(@RequestParam(value = "page", defaultValue = "1") Integer pageno,
                            @RequestParam(value = "limit", defaultValue = "5") Integer pagesize,
                            String username, String phone, String email, String rid) {
        Map<String, Object> paramMap = new HashMap();
        paramMap.put("pageno", pageno);
        paramMap.put("pagesize", pagesize);

        //判断是否为空
        if (!StringUtils.isEmpty(username)) paramMap.put("username", username);
        if (!StringUtils.isEmpty(phone)) paramMap.put("phone", phone);
        if (!StringUtils.isEmpty(email)) paramMap.put("email", email);
        if (!StringUtils.isEmpty(rid) && !rid.equals("0")) paramMap.put("rid", rid);

        PageBean<User> pageBean = adminService.queryPage(paramMap);

        Map<String, Object> rest = new HashMap();
        rest.put("code", 0);
        rest.put("msg", "");
        rest.put("count", pageBean.getTotalsize());
        rest.put("data", pageBean.getDatas());
        return rest;
    }

    /**
     * 跳转添加管理员页面
     *
     * @return
     */
    @GetMapping("/addAdmin")
    public String addAdmin(String type, String id, Model model) {
        if (type != null && type.equals("edit")) {
            User user = adminService.selectById(id);
            model.addAttribute(SessionKeyConst.USER, user);
        }
        return "manager/admin/addAdmin";
    }

    /**
     * 添加管理员  修改管理员
     *
     * @param user
     * @param status
     * @return
     */
    @PostMapping("/addAdmin")
    @ResponseBody
    public CommonResult submitAddAdmin(User user, String status) {
        User byName = adminService.selectByName(user.getUsername());
        User byEmail = adminService.selectByEmail(user.getEmail());
        if (user.getRid().equals(0)) {
            commonResult.ajaxFalse("请选择角色");
            return commonResult;
        }
        //on 表示通过 null 表示待审核
        user.setStatus(status != null ? 1 : 0);
        if (user.getId() != null) {
            //修改管理员
            if (byName != null && !byName.getId().equals(user.getId())) {
                //与修改用户名一样，但存在数据库中，表示后来改的用户名已存在
                commonResult.ajaxFalse("用户名已存在");
                return commonResult;
            }
            if (byEmail != null && !byEmail.getId().equals(user.getId())) {
                //与修改邮箱一样，但存在数据库中，表示后来改的邮箱已存在
                commonResult.ajaxFalse("邮箱已存在");
                return commonResult;
            }
            int count = adminService.editByAdmin(user);
            if (count > 0) {
                commonResult.ajaxTrue("修改成功");
            } else {
                commonResult.ajaxFalse("修改失败");
            }
        } else {
            //添加管理员
            if (byName != null) {
                //与原用户名不一样，但存在数据库中，表示后来改的用户名已存在
                commonResult.ajaxFalse("用户名已存在");
                return commonResult;
            }
            if (byEmail != null) {
                //与原邮箱不一样，但存在数据库中，表示后来改的邮箱已存在
                commonResult.ajaxFalse("邮箱已存在");
                return commonResult;
            }
            String stringDate = DateUtil.today();
            user.setCreatetime(stringDate);
            user.setPassword(passwordEncoder.encode("123")); // 设置默认密码
            int count = adminService.insertAdmin(user);
            if (count > 0) {
                commonResult.ajaxTrue("添加成功");
            } else {
                commonResult.ajaxFalse("添加失败");
            }
        }
        return commonResult;
    }

    /**
     * 删除管理员
     *
     * @param data
     * @return
     */
    @PostMapping("/delAdmin")
    @ResponseBody
    public CommonResult delAdmin(Data data) {
        int count = adminService.delByAdminIds(data.getIds());
        if (count >= data.getIds().size()) {
            commonResult.ajaxTrue("删除成功");
        } else {
            commonResult.ajaxFalse("删除失败");
        }
        return commonResult;
    }

}
